package com.yt.sys.api.client;

import com.yt.sys.common.config.AlipayConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.sys.models.service.IPaymentOrderService;

import javax.annotation.Resource;

/**
 * 支付测试接口控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/payment")
@Api(value = "PaymentTestController", tags = "支付测试接口")
public class PaymentTestController {

    @Resource
    private AlipayConfig alipayConfig;
    
    // 注入支付订单服务
    @DubboReference
    private IPaymentOrderService paymentOrderService;

    /**
     * 测试发起支付
     *
     * @param userId      用户ID
     * @param amount      订单金额
     * @param subject     订单标题
     * @param body        订单描述
     * @return 支付页面表单
     */
    @ApiOperation("测试发起支付")
    @GetMapping(value = "/testPay", produces = "text/html")
    public String testPay(
            @RequestParam Long userId,
            @RequestParam Double amount,
            @RequestParam String subject,
            @RequestParam String body) {
        try {
            log.info("测试发起支付宝支付请求，用户ID: {}, 金额: {}, 标题: {}, 描述: {}", userId, amount, subject, body);
            
            // 创建支付订单
            String outTradeNo = paymentOrderService.createPaymentOrder(userId, amount);
            log.info("生成支付订单，订单号: {}", outTradeNo);
            
            String result = alipayConfig.pay(outTradeNo, String.valueOf(amount), subject, body);
            log.info("支付宝支付请求成功，返回结果长度: {}", result != null ? result.length() : 0);
            return result;
        } catch (Exception e) {
            log.error("支付请求异常", e);
            return "支付请求失败: " + e.getMessage();
        }
    }
}